<?php
/**
 * 卡片计算
 * User: Acer
 * Date: 2022/7/23
 * Time: 13:32
 */
namespace app\crontab\controller;

use app\card\models\UserCardModel;
use app\crontab\service\CardShellService;

class CardCensus{
//    protected $noNeedAction = ['*']; // 不需要校验控制器，通道
//    // 操作前置
//    protected $beforeActionList = ['my_initialize'];

    // 操作前置方法
    public function my_initialize()
    {
//        $this->is_admin();  // 权限检查
    }

    //定时统计当天数据
    public function census_card(){
        $data = input();
        // 是否开启重写
        if( isset($data['rewrite']) && !empty($data['rewrite']) && $data['rewrite'] == 'call' ){
            $UserCardModel =  new UserCardModel();
            // 起始时间
            $min_id=$UserCardModel->min("cu_id");
            $min_id = obj_to_arr($min_id);
            if(!$min_id) exit();
            $createTime = $UserCardModel->where("cu_id",$min_id)->value("creation_time");
//            // 开始时间
            $day_start = strtotime(date('Y-m-d',strtotime($createTime)));
            // 结束 时间
            $day_end = strtotime(date('Y-m-d'));
            for($i=$day_start;$i<$day_end;$i=$i +86400) {
                $temp_day=date('Y-m-d',$i);
                $this->census($temp_day);
            }
        }else{
            $this->census();
        }
        exit();
    }

    private function census($new_time = null){
        $CardShellService =  new CardShellService();
        $census =[];
        // 统计日期
        $census['census_time']= $new_time ? $new_time : date('Y-m-d', strtotime('-1 day', time()));  // 默认前一天
        // 卡片总数
        $census['total_number'] =$CardShellService->s_card_add_total([
            'creation_time' =>['<=',$census['census_time']." 23:59:59"]
        ]);
        // 总激活卡片数
        $census['active_number'] = $CardShellService->s_card_active_total([
            'creation_time' =>['<=',$census['census_time']." 23:59:59"]
        ]);

         // 卡分类
         $cardTypeNumber = $CardShellService->s_active_card_total([
            'creation_time' =>['<=',$census['census_time']." 23:59:59"],
         ]);

        // 家庭卡激活数
        $census['active_family_number'] = $cardTypeNumber['family_number'];
        // 个人卡激活数
        $census['active_personal_number'] = $cardTypeNumber['personal_number'];
        $census['active_qmx_number'] = $cardTypeNumber['qmx_number'];
        $census['active_my_number'] = $cardTypeNumber['my_number'];


// ===============================================================================================
        // 今日新增卡片
        $census['add_number'] =$CardShellService->s_card_add_total([
            'creation_time' =>['like',$census['census_time'].'%']
        ]);
        // 今日新增激活卡片数分类
        $cardDayTypeNumber = $CardShellService->s_active_card_total([
            'creation_time' =>['like',$census['census_time'].'%'],
        ]);
        // 今日家庭卡激活数
        $census['day_active_family_number'] = $cardDayTypeNumber['family_number'];
        // 今日个人卡激活数
        $census['day_active_personal_number'] = $cardDayTypeNumber['personal_number'];
        $census['day_active_qmx_number'] = $cardDayTypeNumber['qmx_number'];
        $census['day_active_my_number'] = $cardDayTypeNumber['my_number'];
        // 今日卡片总激活数
        $census['day_active_number'] = (int)$cardDayTypeNumber['family_number'] + (int)$cardDayTypeNumber['personal_number'] + (int)$cardDayTypeNumber['qmx_number'] + (int)$cardDayTypeNumber['my_number'];
        // 存储到数据库
        $CardShellService->s_save_census($census);
    }
}